/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.mediaplayerdemo;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

/**
 * DatabaseHelper帮助类；
 */
public final class DBUtils {

    private static DatabaseHelper sDatabaseHelper;
    private static volatile DBUtils _instance;
    //单次字符串存储最大长度
    public static final int MAX_STRING_LENGTH= 8096;

    private static final int SP_STRING_MAX_LEN = 8192;

    private DBUtils() {
    }

    /**
     * getInstance
     * @return DBUtils
     */
    public static DBUtils getInstance() {
        if (_instance == null) {
            synchronized (DBUtils.class) {
                if (_instance == null) {
                    _instance = new DBUtils();
                }
            }
        }
        return _instance;
    }

    /**
     * register
     * @param context context
     */
    public void register(Context context) {
        sDatabaseHelper = new DatabaseHelper(context.getApplicationContext());
    }

    /**
     * getDatabaseHelper
     * @param fileName fileName
     * @return Preferences
     */
    public Preferences getDatabaseHelper(String fileName) {
        return sDatabaseHelper.getPreferences(fileName);
    }

    /**
     * 存储超长字符串
     * value分成多片，key_count保存value分片个数
     * 每一片value的键值为"key_i"
     * @param fileName fileName
     * @param key key
     * @param value value
     */
    public void putStringBy8K(String fileName, String key, String value) {
        Preferences preferences = getDatabaseHelper(fileName);
        if (preferences == null || key == null || key.isEmpty()) {
            return;
        }
        deleteString(fileName, key);

        int len = value.length();
        if (len < SP_STRING_MAX_LEN) {
            putString(fileName, key, value);
        }

        int count = (int) Math.ceil(len * 1.0 / SP_STRING_MAX_LEN);
        for (int i = 0; i < count; i++) {
            preferences.putString(key + "_" + i, value.substring(i * SP_STRING_MAX_LEN, (i + 1) * SP_STRING_MAX_LEN));
        }
        preferences.putInt(key + "_count", count);
        // 异步flash
        preferences.flush();
    }

    /**
     * deleteString
     * @param fileName fileName
     * @param key key
     */
    public void deleteString(String fileName, String key) {
        Preferences preferences = getDatabaseHelper(fileName);
        if (preferences == null || key == null || key.isEmpty()) {
            return;
        }
        preferences.delete(key);
        int count = preferences.getInt(key + "_count", 0);
        for (int i = 0; i < count; i++) {
            preferences.delete(key + "_" + i);
        }
        preferences.flushSync();
    }

    /**
     * 获取字符串，拼接
     * @param fileName fileName
     * @param key key
     * @return String
     */
    public String getStringBy8K(String fileName, String key) {
        Preferences preferences = getDatabaseHelper(fileName);
        if (preferences == null || key == null || key.isEmpty()) {
            return "";
        }
        String str = getString(fileName, key);
        if (!(str == null || str.isEmpty())) {
            return str;
        }
        int count = preferences.getInt(key + "_count", 0);
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < count; i++) {
            builder.append(preferences.getString(key + i, ""));
        }
        return builder.toString();
    }

    /**
     * putString
     * @param fileName fileName
     * @param key key
     * @param value value
     */
    public void putString(String fileName, String key, String value) {
        if (value != null && value.length() > SP_STRING_MAX_LEN) {
            return;
        }
        Preferences preferences = getDatabaseHelper(fileName);
        if (preferences == null) {
            return;
        }
        preferences.putString(key, value);
        // 异步flash
        preferences.flush();
    }

    /**
     * getString
     * @param fileName fileName
     * @param key key
     * @return String
     */
    public String getString(String fileName, String key) {
        Preferences preferences = getDatabaseHelper(fileName);
        if (preferences == null) {
            return "";
        }
        return preferences.getString(key, "");
    }

}
